package com.tsystems.clientEmployee.gui;

import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import java.util.Set;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;

import com.toedter.calendar.JDateChooser;
import com.tsystems.entities.Passenger;

/**
 * @author vlad
 * 
 */
public class AddPassengerPanel extends JPanel {

	private static final long serialVersionUID = 5706735981492285638L;
	private JPasswordField password;
	private JTextField loginTxt;
	private JDateChooser birthdayJDC;
	private JTextField nameTxt;
	private JTextField surnameTxt;

	private Passenger result;
	private JPasswordField confirm;

	public AddPassengerPanel() {
		super(new GridBagLayout());
		setBorder(new EmptyBorder(10, 10, 5, 10));

		add(new JLabel("Имя пользователя"), new GridBagConstraints(0, 0, 1, 1,
				0.0, 0.0, GridBagConstraints.CENTER,
				GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 10, 10));
		loginTxt = new JTextField();
		loginTxt.setColumns(15);
		add(loginTxt, new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0,
				GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
				new Insets(0, 0, 0, 0), 0, 0));

		add(new JLabel("Пароль"), new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,
				GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
				new Insets(0, 0, 0, 0), 10, 10));
		password = new JPasswordField();
		add(password, new GridBagConstraints(1, 1, 1, 1, 1.0, 0.0,
				GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
				new Insets(0, 0, 0, 0), 0, 0));

		add(new JLabel("Подтверждение пароля"), new GridBagConstraints(0, 2, 1,
				1, 0.0, 0.0, GridBagConstraints.CENTER,
				GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 10, 10));
		confirm = new JPasswordField();
		add(confirm, new GridBagConstraints(1, 2, 1, 1, 1.0, 0.0,
				GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
				new Insets(0, 0, 0, 0), 0, 0));

		add(new JLabel("Имя"), new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0,
				GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
				new Insets(0, 0, 0, 0), 10, 10));
		nameTxt = new JTextField();
		add(nameTxt, new GridBagConstraints(1, 3, 1, 1, 1.0, 0.0,
				GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
				new Insets(0, 0, 0, 0), 0, 0));

		add(new JLabel("Фамилия"), new GridBagConstraints(0, 4, 1, 1, 0.0, 0.0,
				GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
				new Insets(0, 0, 0, 0), 10, 10));
		surnameTxt = new JTextField();
		add(surnameTxt, new GridBagConstraints(1, 4, 1, 1, 1.0, 0.0,
				GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
				new Insets(0, 0, 0, 0), 0, 0));

		add(new JLabel("Дата рождения"), new GridBagConstraints(0, 5, 1, 1,
				0.0, 0.0, GridBagConstraints.CENTER,
				GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 10, 10));
		birthdayJDC = new JDateChooser(new Date());
		add(birthdayJDC, new GridBagConstraints(1, 5, 1, 1, 1.0, 0.0,
				GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
				new Insets(0, 0, 0, 0), 0, 0));

		JButton okBtn = new JButton("Ок");
		JButton cancelBtn = new JButton("Отмена");
		JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		buttonPanel.add(okBtn);
		buttonPanel.add(cancelBtn);

		add(buttonPanel, new GridBagConstraints(0, 7, 2, 1, 1.0, 0.0,
				GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
				new Insets(0, 0, 0, 0), 0, 0));

		okBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Passenger passenger = new Passenger(loginTxt.getText(),
						new String(password.getPassword()), nameTxt.getText(),
						surnameTxt.getText(), birthdayJDC.getDate());
				result = passenger;

				if (validate(result)) {
					AddPassengerPanel.this.getTopLevelAncestor().setVisible(
							false);
				}
			}
		});

		cancelBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				result = null;
				AddPassengerPanel.this.getTopLevelAncestor().setVisible(false);
			}
		});
	}
	public AddPassengerPanel(Passenger passenger) {
		this();
		loginTxt.setText(passenger.getLogin());
		nameTxt.setText(passenger.getName());
		surnameTxt.setText(passenger.getSurname());
		birthdayJDC.setDate(passenger.getBirthday());
	}
	

	private boolean validate(Passenger passenger) {
		String confirmString = new String(confirm.getPassword());

		if (passenger.getLogin().isEmpty() || passenger.getPassword().isEmpty()
				|| confirmString.isEmpty()) {
			JOptionPane.showMessageDialog(AddPassengerPanel.this,
					"Заполните поля обязательные для заполнения!");
			return false;
		}

		if (!passenger.getPassword().equals(confirmString)) {
			JOptionPane.showMessageDialog(AddPassengerPanel.this,
					"Пароль и его подтверждение не совпадают.");
			return false;
		}
		
		ValidatorFactory vf = Validation.buildDefaultValidatorFactory();
		Validator validator = vf.getValidator();

		String message = validate(passenger, validator);
		if (message != null) {
			JOptionPane.showMessageDialog(AddPassengerPanel.this, message);
			return false;
		}

		

		return true;
	}
	
	
	
	public String validate(Object object, Validator validator) {
		Set<ConstraintViolation<Object>> constraintViolations = validator
				.validate(object);
		String resultString = null;

		if (constraintViolations.size() > 0) {
			resultString = "";

			for (ConstraintViolation<Object> cv : constraintViolations) {
				resultString += cv.getMessage() + "\n";
			}
		}
		return resultString;
	}
	

	private Passenger getResult() {
		return result;
	}

	public static Passenger showDialog(Window owner) {
		JDialog dialog = new JDialog(owner, "Добавление пассажира");
		dialog.setModal(true);
		AddPassengerPanel panel = new AddPassengerPanel();

		dialog.setContentPane(panel);
		dialog.pack();
		dialog.setLocationRelativeTo(null);
		dialog.setVisible(true);

		return panel.getResult();
	}
	
	public static Passenger showDialog(Window owner,Passenger passenger) {
		JDialog dialog = new JDialog(owner, "Изменение пассажира");
		dialog.setModal(true);
		AddPassengerPanel panel = new AddPassengerPanel(passenger);

		dialog.setContentPane(panel);
		dialog.pack();
		dialog.setLocationRelativeTo(null);
		dialog.setVisible(true);

		return panel.getResult();
	}
}
